const app = getApp()
var {
  http
} = require('../../utils/apiutils');
const max_seconds = 59;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    telphone: "",
    verificationCode: "",
    isSend: false,
    seconds: 0,
    type: -1,
    idNumber: '',
    // loginDisabled:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    if (options.idNumber) {
      this.setData({
        idNumber: options.idNumber
      })
    }
  },
  resetuser() {
    if (!this.data.telphone || !this.data.verificationCode || !this.data.idNumber) {
      wx.showToast({
        title: '电话、验证码、身份证号不能为空',
        icon: "none"
      })
    }
    let data = {
      telphone: this.data.telphone,
      verificationCode: this.data.verificationCode,
      idNumber: this.data.idNumber
    }
    wx.login({
      success(res) {
        data.code = res.code;
        wx.showLoading()
        http.updateAccount(data,res => {
          if (res.status == 0) {
            wx.hideLoading()
            wx.showToast({
              title: '绑定成功',
            })
            wx.nextTick(_ => {
              wx.reLaunch({
                url: '/pages/index/index',
              })
            })
          } else {
            wx.hideLoading()
            wx.showToast({
              title: res.message,
              icon:"none"
            })
          }
        },(err)=>{
          console.log(err)
        })
      },
    })

  },
  onclickLogin: function () {
    if (this.data.telphone.length == 11 && this.data.verificationCode.length == 6) {
      if (this.data.idNumber) {
        this.resetuser()
        return false;
      }

      var data = {
        telphone: this.data.telphone,
        verificationCode: this.data.verificationCode,
      }
      if (this.data.type == 4) {
        data["openId"] = app.globalData.openId
        var that = this;
        http.weChatUpdatePhone(data,
          function (res) {
            console.log("----绑定手机号返回的数据--------", res);
            if (res.status == 200) {
              app.globalData.userInfo = res.data;
              app.globalData.token = res.data.token;
              app.globalData.isLogin = true;

              if (res.data.idNumber && res.data.idNumber > 10) {
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                wx.navigateTo({
                  url: '/pages/realname/realname',
                })
              }
            } else {
              wx.showToast({
                title: res.message,
                icon: "none",
              })
            }
          },
          function (err) {

          })

      } else {
        var that = this;
        http.updateTelphone(data,
          function (res) {
            if (res.status == 200) {
              app.globalData.userInfo.telphone = that.data.telphone;
              app.globalData.isLogin = true
              wx.showModal({
                title: '修改成功',
                content: '您已成功修改手机号',
                showCancel: false,
                confirmText: "我知道了",
                success(res) {
                  wx.navigateBack({
                    delta: 2,
                  })
                }
              })
            } else {
              wx.showToast({
                title: res.message,
                icon: "none",
              })
            }
          },
          function (err) {

          })

      }

    } else {
      wx.showToast({
        title: '请填写完信息后提交',
        icon: "none",
      })
    }
  },
  getUserInfo: function () {
    var that = this;
    http.userInfo(data,
      function (res) {
        console.log("------获取用户信息---", res);
        if (res.status == 200) {
          app.globalData.userInfo = res.data;
          wx.navigateBack({
            delta: 1,
          })
          wx.showToast({
            title: "登录成功",
            icon: "none",
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {

      })


  },
  checkPhone: function (phone) {
    console.log(phone);
    if (!(/^1(3|4|5|6|7|8|9)d{9}$/.exec(phone))) {
      return false;
    } else {
      return true;
    }
  },
  bindinputt: function (e) {
    this.data.telphone = e.detail.value;
  },
  bindinputc: function (e) {
    this.data.verificationCode = e.detail.value;
  },
  dongtai: function () {
    var that = this;
    var seconds = max_seconds;
    this.setData({
      isSend: true,
      seconds: seconds,
    })
    var t = setInterval(function () {
      // 如果秒数小于0
      if (seconds <= 0) {
        // 停止定时器
        clearInterval(t);
        // 显示发送按钮
        that.setData({
          isSend: false
        })
        // 停止执行
        return;
      }
      // 秒数减一
      seconds--;
      // 更新当前秒数
      that.setData({
        seconds: seconds
      })
      // console.log(that.globalData.seconds)
    }, 1000)
  },

  onclickCode: function () {

    if (this.data.telphone.length == 11) {
      this.dongtai();
      var data = {
        "telphone": this.data.telphone,
        "type": this.data.type,
      }
      var that = this;
      http.getVerificationCode(data,
        function (res) {
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        },
        function (err) {

        })
    } else {
      wx.showToast({
        title: '请填写正确的手机号',
        icon: "none",
      })

    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (getApp().globalData.userInfo && this.data.type == 4) {
      //  wx.navigateBack({
      //    delta: 1,
      //  })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})